package com.liuxu.autorun;

import android.os.Handler;
import android.os.Message;

import org.json.JSONException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Iden {
    public boolean run( String vision,String token,Handler handler){
        int allow=1;
        Message msg = Message.obtain();
        service server = new service();
        String idurl="http://liuxu.xyz/json/identification";
        String reurl="http://liuxu.xyz/json/register";
        String caurl="http://liuxu.xyz/json/cancel";
        String upurl="http://liuxu.xyz/json/updatastatus";
        String tourl="http://liuxu.xyz/course/";
        Map topics= new HashMap();
        topics.put("注册",reurl);
        topics.put("运行",idurl);
        topics.put("解绑",caurl);
        topics.put("200","成功");
        topics.put("100","成功");
        topics.put("10","注册码错误");
        String data = null;
        String csrf_token= null;
        String updatastatus=null;
        try {
            csrf_token =server.gettoken(tourl);
        } catch (Exception e) {
            msg.obj = "无法联网";
            handler.sendMessage(msg);
        }
        Map values = new HashMap();
        values.put("token", "6666");
        values.put("id","");
        values.put("vision",vision);
        values.put("csrfmiddlewaretoken",csrf_token);
        try {
            data=server.postByOkHttp(topics.get(reurl).toString(),values);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String code=null;
        try {
            code=server.getjson(data,"resultcode");
            msg.obj = "运行"+topics.get(code).toString();
            handler.sendMessage(msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
            String latestvision= null;
            try {
                latestvision = server.getjson(data,"vision");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (!vision.equals(latestvision)){
                allow=0;
                msg.obj = "请更新版本";
                handler.sendMessage(msg);
            }
        if (code.equals("100")&allow==1){
            return true;
        }
        return false;
    }
}
